/**
 * 
 */
package com.et114.modules.system.dictionary.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.CONST;
import com.et114.core.common.Error;
import com.et114.core.utility.GetterUtil;
import com.et114.modules.org.message.OrgMessageManager;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.form.BizOptionForm;
import com.et114.modules.system.dictionary.service.DictionaryFactory;
import com.et114.modules.system.dictionary.service.IDictionaryService;
import com.et114.modules.system.dictionary.vo.BizOption;
import com.et114.modules.system.dictionary.vo.BizType;


import org.apache.commons.beanutils.PropertyUtils;

import java.util.List;
import java.util.StringTokenizer;
/*
 * @author guanhw
 */
public class DicOptionAction extends BaseDispatchAction{
	
	public  ActionForward loadDicOptions( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		BizOptionForm bizoForm = ( BizOptionForm ) form ;
		IDictionaryService bean = DictionaryFactory.getInstansce ( ) ; 
		
		String bizTypekey = bizoForm.getBizTypeKey ( ) ;
		String bizTypeId =  GetterUtil.get ( request.getParameter ( "bizTypeId" ) , null ) ;
		if ( bizTypeId != null  ) {
			BizType bizType = bean.retrieveBizType ( bizTypeId ) ; 
			bizTypekey = bizType.getBizTypeKey ( ) ; 
			request.setAttribute ( "bizType" , bizType ) ;
			List options = bean.retrieveBizOptionList ( bizTypekey , DictionaryCache.BIZOPTION_STATUS_ALL ) ;
			request.setAttribute ( "options" , options ) ;			
		}
		return mapping.findForward ( "options" );
	}
	
	
	public  ActionForward newDicOption( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		BizOptionForm bizoForm = ( BizOptionForm ) form ;
		IDictionaryService bean = DictionaryFactory.getInstansce ( ) ; 
		
		String bizTypeId =  GetterUtil.get ( request.getParameter ( "bizTypeId" ) , null ) ;
		if ( bizTypeId != null  ) {
			BizType bizType = bean.retrieveBizType ( bizTypeId ) ;
			List options = bean.retrieveBizOptionList ( bizType.getBizTypeKey ( ) , DictionaryCache.BIZOPTION_STATUS_ALL ) ; 
			BizOption  option = new BizOption ( ) ; 
			PropertyUtils.copyProperties ( option , bizoForm ) ;
			option.setBizTypeKey ( bizType.getBizTypeKey ( ) ) ;
			option.setDisplOrder ( new Integer( options.size ( ) + 1 ) ) ; 
	
			if ( ( bean.countBizOption ( option ) ).longValue ( ) > 0 || ( bean.countBizOptionValue ( option ) ).longValue ( ) > 0 ) {
				Error error = new Error();
				error.setErrorTitle ( OrgMessageManager.get().getMessage("org.bizoption.already.exist") ) ;
				request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
				return mapping.findForward( "glerror" ) ;
			}
			bean.addBizOption ( option ) ;
		}
		return new ActionForward( "bizSuccess.go?type=option&bizTypeId=" + bizTypeId  , true ) ;
	}
	
	public  ActionForward modDicOption( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		BizOptionForm bizoForm = ( BizOptionForm ) form ;
		IDictionaryService bean = DictionaryFactory.getInstansce ( ) ; 
		
		String bizTypeId =  GetterUtil.get ( request.getParameter ( "bizTypeId" ) , null ) ;
		if ( bizTypeId != null  ) {
			BizType bizType = bean.retrieveBizType ( bizTypeId ) ;
			BizOption  option = new BizOption ( ) ; 
			PropertyUtils.copyProperties ( option , bizoForm ) ;
			option.setBizTypeKey ( bizType.getBizTypeKey ( ) ) ;
			BizOption bo = bean.retrieveBizOption(option.getOptionId());
			if (!bo.getOptionName().equals(option.getOptionName())) {
				if ( ( bean.countBizOption ( option ) ).longValue ( ) > 0 ) {
					Error error = new Error();
					error.setErrorTitle ( OrgMessageManager.get().getMessage("org.bizoption.already.exist") ) ;
					request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
					return mapping.findForward( "glerror" ) ;
				}
			}
			if ( !bo.getOptionValue ( ).equals ( option.getOptionValue ( ) ) ){
				if ( ( bean.countBizOptionValue ( option ) ).longValue ( ) > 0 ) {
					Error error = new Error();
					error.setErrorTitle ( OrgMessageManager.get().getMessage("org.bizoption.already.exist") ) ;
					request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
					return mapping.findForward( "glerror" ) ;
				}
			}
			if (!bo.getOptionName().equals(option.getOptionName()) && !bo.getOptionValue ( ).equals ( option.getOptionValue ( ) )){
				if ( ( bean.countBizOption ( option ) ).longValue ( ) > 0  || ( bean.countBizOptionValue ( option ) ).longValue ( ) > 0) {
					Error error = new Error();
					error.setErrorTitle ( OrgMessageManager.get().getMessage("org.bizoption.already.exist") ) ;
					request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
					return mapping.findForward( "glerror" ) ;
				}
			}
			bean.updateBizOption (  option ) ;
			/*
			to.append ( "&bizTypeId=" ).append ( bizType.getBizTypeId ( ) ) ; 
			to.append ( "&optionId=" ).append ( bizoForm.getOptionId( ) ) ;
			to.append ( "&msg=2" ) ;
			*/
		}
		else throw new Exception( "bizTypekey is null !" ) ; 
		return mapping.findForward ( "glsuccess" ) ;
		//return new ActionForward( to.toString ( ) , true  ) ; 
	}	
	
	public  ActionForward loadDicOption( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		BizOptionForm bizoForm = ( BizOptionForm ) form ;
		IDictionaryService bean = DictionaryFactory.getInstansce ( ) ; 

		String optionId =  GetterUtil.get (  bizoForm.getOptionId ( ) , null ) ;
		if ( optionId != null ) {
			BizOption  option = bean.retrieveBizOption ( optionId ) ;
			request.setAttribute ( "option" , option ) ; 
		}
		String bizTypeId = GetterUtil.get (  request.getParameter ( "bizTypeId" ) , null ) ;
		if ( bizTypeId != null ) {
			BizType bizType = bean.retrieveBizType ( bizTypeId ) ; 
			request.setAttribute ( "bizType" , bizType ) ;			
		}
		
		request.setAttribute ( "bizTypeId" , bizTypeId ) ; 
		return mapping.findForward ( "newOption" ) ; 
	}		

	public  ActionForward removeDicOption( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		BizOptionForm bizoForm = ( BizOptionForm ) form ;
		IDictionaryService bean = DictionaryFactory.getInstansce ( ) ; 
		
		Long[] deleteId = bizoForm.getDeleteId ( ) ; 
		if ( deleteId != null  ) {
			for ( int i = 0 ; i < deleteId.length ; i ++ ) {
				bean.deleteBizOption ( deleteId[ i ].toString ( ) , true ) ; 
			}
		}
		String bizTypeId = request.getParameter ( "bizTypeId" ) ; 
		return new ActionForward( "bizdicOption.go?method=loadDicOptions&bizTypeId=" + bizTypeId , true ) ; 
	}	
	
	public  ActionForward toOrderOptions( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		loadDicOptions( mapping , form , request , response  ) ; 
		return mapping.findForward ( "order" );
	}	
	
	
	public  ActionForward orderOptions( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		StringBuffer to = new StringBuffer( ) ; 
		BizOptionForm bizoForm = ( BizOptionForm ) form ;
		IDictionaryService bean = DictionaryFactory.getInstansce ( ) ; 

		String orders = bizoForm.getOrders ( ) ; 
		StringTokenizer st = new StringTokenizer( orders , ";" ) ;
		int count = st.countTokens ( ) ; 
		for (  int i = 0 ; i <  count ; i ++ ) {
			String optionId = st.nextToken();
			BizOption option = bean.retrieveBizOption ( optionId ) ;
			option.setDisplOrder ( new Integer( i + 1 ) ) ;
			bean.updateBizOption ( option ) ;
		}
		String bizTypeId = request.getParameter ( "bizTypeId" ) ;
		to.append ( "bizdicOption.go?method=toOrderOptions" ) ; 
		to.append ( "&bizTypeId=" ).append ( bizTypeId ) ;
		return new ActionForward( to.toString ( ) , true ) ;
	}
}
